using Godot;
using System;
using Godot.Collections;
using System.Threading.Tasks.Dataflow;

public partial class Game : Node
{
	public const string SAVE_PATH = "user://scp/data.sav";
	private Dictionary<string, Variant> _loadData;//临时读取文件
	public Dictionary<string, Variant> LoadData{get; set;}//公开获取
	public Player _player;
	private MapManager _mapManager;
	private Main main;
	private Map _map;
	private Map oldmap;
	private Map newmap;
	public Timer _timer;
	public enum update_scenes
	{
		Main,
		home,
		zoulang
	}
    public override void _Ready()
    {
        base._Ready();
		update_scenes_enemies();
		//_player = GetNode<Player>("%Player");
		_timer = GetNode<Timer>("Timer");
		_timer.OneShot = true;
		_mapManager = GetTree().CurrentScene.GetNode<MapManager>("%MapManager");
    }
    public override void _PhysicsProcess(double delta)
    {
        base._PhysicsProcess(delta);
		
    }
    public void change_scene(string path , string EnterPoint)
	{
		var tree = GetTree();

		main = (Main)tree.CurrentScene;
		var childNodes = main.GetChildren();
		oldmap = get_map();
		//tree.ChangeSceneToFile(path);
		if(oldmap != null)
		{oldmap.QueueFree();}
		PackedScene packedScene = GD.Load<PackedScene>(path);
		newmap = (Map)packedScene.Instantiate();
		_mapManager.AddChild(newmap);

		foreach (Enterpoint node in tree.GetNodesInGroup("EnterPoints"))
		{
			Node rootNode = tree.CurrentScene;

			_map = (Map)_mapManager.GetChild(0);
			
		
			if (node.Name == EnterPoint)
			{
				_map.Update_Player(node.GlobalPosition);
			}
		}
	}

	public void update_scenes_enemies()
	{}
	
	public void save_game()
	{
		var DataForSave = new Dictionary<string, Variant>();
		{
			//{"player", [Dictionary]};
		}

		var saveFileForWrite = FileAccess.Open(SAVE_PATH, FileAccess.ModeFlags.Write);//打开文件进行写入
		var playerDataForsave = _player.GetDataForSave();
		var SceneControl = GetNode<SceneControl>("/root/SceneControl");
		var mapDataForsave = SceneControl.get_scenes();

		DataForSave["player"] = playerDataForsave;
		DataForSave["map"] = mapDataForsave;

		saveFileForWrite.StoreVar(DataForSave,true);

		saveFileForWrite.Dispose();//释放句柄

	}

	public Main get_main()
	{
		return GetTree().CurrentScene as Main;
	}

	public Map get_map()
	{
		var main = get_main();

		var childNodes = main.GetNode<MapManager>("%MapManager").GetChildren();
		//Map map;

		foreach (var child in childNodes)
		{
			if (child is Map)
			{
				_map = (Map)child;
			}
			
		}
		return _map;
		
	}

	public Player get_player()
	{
		var main = get_main();
		Player player = main.GetNode<Player>("%Player");
		return player;
	}

	public MessageManager get_message_manager()
	{
		var main = get_main();
		MessageManager node = main.GetNode<MessageManager>("%MessageManager");
		return node;
	}

	public Node InstantScene(string scenePath)
	{
		var scene = GD.Load<PackedScene>(scenePath);
		Node newscene = scene.Instantiate() as Node;
		return newscene;
	}

	public Item LoadItem(string itemName)
	{
		string basepath = "res://resources/items/";
		var path = basepath + itemName + ".tres";
		Item item = GD.Load<Item>(path);
		return item;
	}

	public void InitAllGameData()
	{}


}
